package pro.shuangxi.model.music.controller;


import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import mybatisplus.service.IBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author mengshx
 * @since 2020-12-27
 */
@Controller
public class MusicController {
    @Autowired
    IBookService bookService;
    @RequestMapping("/randomMusic.do")
    public void randomMusic(HttpServletResponse response) throws IOException {

        HttpResponse httpResponse = HttpUtil.createGet("https://api.uomg.com/api/rand.music").execute();
        String location = httpResponse.header("location");
        httpResponse = HttpUtil.createGet(location).execute();
        location = httpResponse.header("location");
        httpResponse = HttpUtil.createGet(location).execute();
        InputStream inputStream = httpResponse.bodyStream();
        ServletOutputStream outputStream = response.getOutputStream();
        byte[] buffer = new byte[1024];
        int len = inputStream.read(buffer);
        while (len != -1) {
            outputStream.write(buffer, 0, len);
            len = inputStream.read(buffer);
        }
    }

}

